import $store from '@/sheep/store';
import association from '@/association/store';
import {
	showAuthModal
} from '@/sheep/hooks/useModal';
import {
	baseUrl,
	platform,
	platformToken
} from '@/sheep/config/association.js';

// Loading全局实例
let LoadingInstance = {
	target: null,
	count: 0,
};

/**
 * 关闭loading
 */
function closeLoading() {
	if (LoadingInstance.count > 0) LoadingInstance.count--;
	if (LoadingInstance.count === 0) uni.hideLoading();
}

function cRequest(config) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + config.url,
			timeout: 10000,
			method: config.method || 'get',
			data: config.data,
			header: {
				Accept: 'text/json',
				'Content-Type': 'application/json;charset=UTF-8',
				token: uni.getStorageSync('associationToken'),
				platform: config.platform ? config.platform : platform,
				...config.header,
			},
			success: (response) => {
				config?.custom?.showLoading && closeLoading();
				if(response.data){
					if (response.data?.code !== 1) {
						let errorMessage = ''
						if (response.data.code == 401) {
							
							if (!!uni.getStorageSync(platformToken)) {
								console.log(config,'----')
								errorMessage = '服务器开小差啦,请稍后再试~';
							} else {
								errorMessage = '请先登录';
							}
							association().login();
							// uni.showToast({
							// 	title: errorMessage,
							// 	icon: 'none',
							// 	mask: true,
							// 	duration: 1500,
							// });
						}
						if (config?.custom?.showError ) {
							// uni.showToast({
							// 	title: response.data.msg || errorMessage,
							// 	icon: 'none',
							// 	mask: true,
							// 	duration: 1500,
							// });
						}
						return reject(response.data);
					}
					if (response.data?.error == 1 && response.data?.msg !== '' && config?.custom
						?.showSuccess) {
						// uni.showToast({
						// 	title: config?.custom?.successMsg || response.data.msg,
						// 	icon: 'none',
						// });
					}
				}
				return resolve(response.data);
			},
			fail: (error) => {
				if (error && config) {
					if (config?.custom?.showError === false) {
						uni.showToast({
							title: error.data?.msg || '网络请求出错',
							icon: 'none',
							mask: true,
						});
					}
					config?.custom?.showLoading && closeLoading();
				}

				return reject(false);
			},
		});
	});
}

async function request(data) {
	let config = {
		method: 'GET',
		...data,
		custom: {
			showError:true,
			...data.custom
		},
	};
	const udid = uni.getStorageSync('udid');
	config.platform = udid ? udid : config.platform
	if (config?.custom?.auth && !uni.getStorageSync(platformToken) && config.url != 'api/user/thirdLogin') {
		association().logout()
		showAuthModal();
		return Promise.reject({code:401});
	} else if (
		config?.custom?.auth &&
		!association().isLogin() &&
		config.url != 'api/user/thirdLogin'
	) {
		await association().login();
	} else if (config.url == 'api/user/thirdLogin' && !config.header.Authorization) {
		showAuthModal();
		return Promise.reject({code:401});
	}
	if (config?.custom?.showLoading) {
		LoadingInstance.count++;
		LoadingInstance.count === 1 &&
			uni.showLoading({
				title: config?.custom?.loadingMsg || '',
				mask: true,
				fail: () => {
					uni.hideLoading();
				},
			});
	}
	return cRequest(config);
}
export default request;